#include<stdio.h>
#define Size 15
#define Row Size
#define Column Size
#define true 1
#define false 0
void Init(char(*Board)[Size])
{
    for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            Board[i][j]='-';
        }
    }

}

int MakeMove(char (*Board)[Column],int row,int column,char player)
{
    if(row<0||row>=Row||column<0||column>=Column)
    {
        printf("invalid place! please try again!\n");
        return false;
    }

    if(Board[row][column]!='-')
    {
        printf("this place have no place! try again!");
        return false;
    }

    Board[row][column]=player;
    return true;
}

void PrintBoard(char(*Board)[Size])
{
    printf("   ");
    for(int i=0;i<Size;i++)
    {
        printf(" %2d",i+1);
    }
    printf("\n");
    for(int i=0;i<Size;i++)
    {
        printf("%2d ",i+1);
        for(int j=0;j<Size;j++)
        {
            printf("%3c",Board[i][j]);
        }
        printf("\n");
    }
}

int CheckIsWin(char(*Board)[Size],int row,int column,char player)
{
    int direction[4][2]={{1,0},
                         {0,1},
                         {1,1},
                         {-1,1}
                        };
        for(int i=0;i<4;i++)
        {
            int count=1;
            int dx=row+direction[i][0];
            int dy=column+direction[i][1];
            while(dx>=0 && dx<Size && dy>=0 && dy<Size && Board[dx][dy]==player)
            {
                    count++;
                    dx=dx+direction[i][0];
                    dy=dy+direction[i][1];
            }
            dx=row-direction[i][0];
            dy=column-direction[i][1];
            while(dx>=0 && dx<Size && dy>=0 && dy<Size && Board[dx][dy]==player)
            {
                    count++;
                    dx=dx-direction[i][0];
                    dy=dy-direction[i][1];
            }
            if(count>=5)
                return true;
        }
        return false;
}



char player='X';
int main()
{
    char Board[Size][Size]={0};
    Init(Board);
    // PrintBoard(Board);
    while(1)
    {
        PrintBoard(Board);

        int row =0,column=0;
        printf("please player%c input a place to make move:",player);
        scanf("%d %d",&row,&column);
        if(MakeMove(Board,row-1,column-1,player)==false)
        {
            continue;
        }

        if (CheckIsWin(Board,row-1,column-1,player)==true)
        {
            // PrintBoard(Board);
            printf("player%c wins!Congratulations!\n",player);
            break;
        }

        player=(player=='X') ? 'O' : 'X';

    }
    return 0;
}